#ifndef RECENTFILESMANAGER_H
#define RECENTFILESMANAGER_H

#include <QObject>

class RecentFile : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString modelName READ modelName WRITE setModelName NOTIFY modelNameChanged)
    Q_PROPERTY(QString modelPath READ modelPath WRITE setModelPath NOTIFY modelPathChanged)

public:
    explicit RecentFile(QObject *parent = nullptr): QObject(parent){}

    const QString &modelName() const{ return m_modelName; }
    void setModelName(const QString &newModelName){
        if (m_modelName == newModelName)
            return;
        m_modelName = newModelName;
        emit modelNameChanged();
    }
    const QString &modelPath() const { return m_modelPath; }
    void setModelPath(const QString &newModelPath){
        if (m_modelPath == newModelPath)
            return;
        m_modelPath = newModelPath;
        emit modelPathChanged();
    }

signals:
    void modelNameChanged();
    void modelPathChanged();

private:
    QString m_modelName;
    QString m_modelPath;
};

#define RF_MAX_SIZE 10

class RecentFilesManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVector<RecentFile*> recentFiles READ recentFiles WRITE setRecentFiles NOTIFY recentFilesChanged)

public:
    explicit RecentFilesManager(QObject *parent = nullptr);
    ~RecentFilesManager();

    const QVector<RecentFile *> &recentFiles() const;
    void setRecentFiles(const QVector<RecentFile *> &newRecentFiles);
    void initRecentFiles();

    Q_INVOKABLE int getRecentFilesSize();
    Q_INVOKABLE QString getModelNameAt(int index) { return m_recentFiles[index]->modelName(); }
    Q_INVOKABLE QString getModelPathAt(int index) { return m_recentFiles[index]->modelPath(); }
    Q_INVOKABLE void pushRecentFile(QString modelPath, QString modelName = "0");

signals:
    void recentFilesChanged();

private:
    QVector<RecentFile *> m_recentFiles;

private:
    void recordRecentFiles();
    bool isFileExist(QString fullFileName);
};

#endif // RECENTFILESMANAGER_H
